{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sisl\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "TranSiesta allows a great variety of contours when performing the equilibrium contours\n",
    "\n",
    "In this example, you will try and create different contours for the equilibrium part.\n",
    "\n",
    "The default contours are defined like this:\n",
    "\n",
    "```\n",
    "%block TS.ChemPot.Left\n",
    "  mu V/2\n",
    "  contour.eq\n",
    "    begin\n",
    "      C-Left\n",
    "      T-Left\n",
    "    end\n",
    "%endblock TS.ChemPot.Left\n",
    "%block TS.ChemPot.Right\n",
    "  mu -V/2\n",
    "  contour.eq\n",
    "    begin\n",
    "      C-Right\n",
    "      T-Right\n",
    "    end\n",
    "%endblock TS.ChemPot.Right\n",
    "\n",
    "TS.Contours.Eq.Pole 2.5 eV\n",
    "%block TS.Contour.C-Left\n",
    "  part circle\n",
    "   from -40. eV + V/2 to -10 kT + V/2\n",
    "     points 25\n",
    "      method g-legendre\n",
    "%endblock TS.Contour.C-Left\n",
    "%block TS.Contour.T-Left\n",
    "  part tail\n",
    "   from prev to inf\n",
    "     points 10\n",
    "      method g-fermi\n",
    "%endblock TS.Contour.T-Left\n",
    "%block TS.Contour.C-Right\n",
    "  part circle\n",
    "   from -40. eV -V/2 to -10 kT -V/2\n",
    "     points 25\n",
    "      method g-legendre\n",
    "%endblock TS.Contour.C-Right\n",
    "%block TS.Contour.T-Right\n",
    "  part tail\n",
    "   from prev to inf\n",
    "     points 10\n",
    "      method g-fermi\n",
    "%endblock TS.Contour.T-Right\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add a temperature gradient\n",
    "\n",
    "The default temperatures of the chemical potentials are the Siesta electronic temperature. TranSiesta enables to distinguish the electrodes by their chemical potential *and* their electronic temperature. I.e. the distribution functions of the electrodes may be *very* different:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def nf(E, mu, kT):\n",
    "    return 1 / (1 + np.exp( (E-mu) / kT ))\n",
    "# Generate two different distributions\n",
    "E = np.linspace(-2, 2, 100)\n",
    "plt.plot(E, nf(E, 0.25, 0.025), label='V=0.25, kT=0.025');\n",
    "plt.plot(E, nf(E, -0.25, 0.05), label='V=-0.25, kT=0.05');\n",
    "plt.plot(E, nf(E, 0.25, 0.025) - nf(E, -0.25, 0.05), '.', label=r'$\\delta n_F$');\n",
    "plt.legend();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When implementing this in the input-options, it will likely fail if one of the electrodes has a temperature higher than the `TS.ElectronicTemperature` (defaults to `ElectronicTemperature`). This is because TranSiesta will fail if one of the tail integrals in the non-equilibrium contour is not at least 5 kT from the respective chemical potential, hence correct the non-equilibrium contour input."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Reduce number of equilibrium contours\n",
    "\n",
    "Instead of using 2 contours *per chemical potential*, one can reduce it to 2 contours (in total). I.e. the chemical potentials shares the same contours. Note, however, in this case one *cannot* use the Gauss-Fermi quadrature for the tail part of the complex contour. Hence choose something different than `g-fermi`.\n",
    "\n",
    "Copy [TS 02](../TS_02/run.ipynb) and adapt the contours such that there are only 2 contours regardless of the applied bias. When testing various contours, you can add `TS.Analyze` to the input to make it quite after the analyzation step. This will also be after the corresponding `siesta.TSCCEQ*` files are created.\n",
    "\n",
    "You can use the following line to read in the contour data-points:\n",
    "\n",
    "    CC = sisl.io.tableSile('<>/siesta.TSCCEQ-Left').read_data()\n",
    "   \n",
    "where `CC` is a matrix variable with 4 rows:\n",
    "\n",
    "| `CC[0, :]` | `CC[1, :]` | `CC[2, :]` | `CC[3, :]` | \n",
    "| :---: | :---: | :---: | :---: |\n",
    "| Real energy  | Imaginary energy | Real weight | Imaginary weight |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
